﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using System.Data.SqlClient;

namespace ChatServer.model
{
    class ServerSocket : dbconn.DBConnector
    {
        //private list of Room objects. One object for each Room registered in Database
        private List<Room> _activeRooms;

        public ServerSocket()
        {
            //to fill _activeRooms, instantiate _activeRooms and call getRooms() method
            _activeRooms = new List<Room>();
            GetRooms();
        }

        public bool Login(string userName, string ipAddress, string password)
        {
            return ValidateUser(userName, ipAddress, password);
        }

        private bool ValidateUser(string userName, string IPAddress, string userPassword)
        {
            //create new User by passing in parameters
            User u = new User(userName, userPassword);

            if (u.Authentication(userName, userPassword))
            {
                CreateUser(u, IPAddress);
                return true;
            }
            else
                //garbage collection will take care of user object that is not used
                return false;
        }

        public void CreateUser(User u, string IPAddress)
        {
            UserSocket us = new UserSocket(IPAddress);
            us.User = u;
            UserSocketToRooms(us);
        }

        public void GetRooms()
        {
            DataTable dt = this.SendDataTableRequest("spGetRooms", new List<SqlParameter>());

            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Room r = new Room(dr["roomName"].ToString());
                    _activeRooms.Add(r);
                }
            }
            else
            {
                throw new Exception("Get Rooms DataTable not created.");
            }
        }

        public void UserSocketToRooms(UserSocket u)
        {
            // TO DO: Determine rooms based on permissions

            foreach (Room r in _activeRooms)
            {
                r.AppendSocket(u);
            }
        }

        // PROCESS MESSAGE

        /*
         * 
         * Instantiate chat obj and pass xml (just deliver, no processing)
         * 
         */
        private Room GetRoom(string roomName)
        {
            try
            {
                foreach (Room r in _activeRooms)
                {
                    if (r.RoomName == roomName)
                    {
                        return r;
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public void ProcessMessage(string xml)
        {
            Chat c = new Chat(xml);
            try
            {
                Room r = GetRoom(c.RoomName); //TO DO: Update chat method name once developed
                r.PassChat(c); //TO DO: Match room method name once developed
            }
            catch (Exception ex)
            {
                c.WriteFUp(); //TO DO: Match chat method for epic error handling once developed
            }

        }

        // LOGOUT

        /*
         * 
         * Invoke logout for room(s)
         * 
         */
        public void LogOut(string ipAddress)
        {
            foreach (Room room in _activeRooms)
            {
                room.RemoveUser(ipAddress);
            }
        }
    }
}
